#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

//创建HTTP客户端
HTTPClient client;
//创建TLS客户端
WiFiClientSecure tls;
//API地址
String url = "https://v2.jinrishici.com/sentence";
//Token码
String token = "o9M10gtneoIxRLk5UO13fUG7guYxnS7h";

void setup() {
  WiFi.mode(WIFI_STA);
  Serial.begin(74880);
  WiFi.begin("LIUYU", "12345678");
  while (!WiFi.isConnected())
  {
    delay(100);
  }
  Serial.println("WiFi连接成功");
  //初始化HTTP客户端
  client.begin(tls, url);
  //不验证服务器证书
  tls.setInsecure();
  //添加请求头部字段
  client.addHeader("X-User-Token", token);

  //发送HTTP请求消息
  int code = client.GET();
  if (code == 200)
  {
    //获取服务器响应消息正文
    String response = client.getString();
    //Serial.println(response);

    //解析代码可以使用arduinojson.org/v6/assistant自动生成
    //将JSON字符串解析为C++对象
    StaticJsonDocument<1024> doc;
    DeserializationError error = deserializeJson(doc, response);
    //解析出错处理
    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }
    //打印推荐内容
    const char* content = doc["data"]["content"];
    Serial.println(content);
  }
  else
  {
    //打印状态码
    Serial.println(code);
  }
  //断开和服务器的连接
  client.end();
}

void loop() {
  // put your main code here, to run repeatedly:

}
